import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.TreeSet;
import java.util.Arrays;

public class PRO {
   private void run() throws Exception {
       
       TreeSet < Integer > ts = new TreeSet < Integer > (); 
       BufferedReader bf = new BufferedReader(new 
                                InputStreamReader(System.in)); 
 
       int count [] = new int [1000001]; 
       Arrays.fill(count,0);
     
       long res = 0;
       int n = Integer.parseInt(bf.readLine());
 
       for(int i=1;i<=n;++i){
          StringTokenizer st = new StringTokenizer(bf.readLine());
          int x = Integer.parseInt(st.nextToken());
          for(int j=1;j<=x;++j){
              int t = Integer.parseInt(st.nextToken());
              count[t]++; ts.add(t);
          }
          int a = ts.first(); int b = ts.last();
          res += b-a; count[a]--; count[b]--;
          if(count[a]==0) ts.remove(a);
          if(count[b]==0) ts.remove(b);
       }
       System.out.println(res);      

   }
   public static void main( String [] Args ) throws Exception {
      new PRO().run();
   }
}



